package com.xcity.game.peakroad.template;

import java.util.Arrays;

import com.xcity.game.combat.attr.CombatAttribute;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.item.DropGroupTemplate;
import com.xcity.game.item.drop.DropGroup;
import com.xcity.game.npc.NpcTeamTemplate;
import com.xcity.game.npc.NpcTemplate;

import naga.x.App;
import naga.x.game.template.Template;
import naga.x.game.template.TemplateException;
import naga.x.game.template.TemplateService;
import naga.x.game.template.annotation.Excel;

/**
 * 巅峰之路
 * @author yang.li
 *
 */
@Excel(value = "peak_road.xlsx", sheet = 0)
public class PeakRoadTemplate extends Template {

	public static final int DIFF_SIMPLE = 0, DIFF_NORMAL = 1, DIFF_HARD = 2;
	public static final byte FLOOR_CHALLENGE = 0/*挑战层*/, FLOOR_REWARD = 1/*奖励层*/;
	
	/** 该层类型 */
	protected byte type = -1;
	
//	/** npc战队id(简单,中等,困难) */
//	protected int[] npcTeamIds;
	protected NpcTeamTemplate[] npcTeams;
	/** 标准战队id */
	protected int npcTeamId;
	
	/** 另外两个难度（简单、困难）的npc数值系数 */
	protected float[] npcTeamRatio;

	/** buffs(按百分比加成) */
	protected int[] buffs;
	protected CombatAttribute[] wrappedBuffs;

	/** 奖励掉落组id */
	protected int[] dropId;
	protected DropGroupTemplate[] dropGroups;
	
	/** 奖励巅峰币 */
	protected int peakCoin;

	/**
	 * 评星条件：剩余hp百分比<br/>
	 * 即剩余血量必须大于（等于）该百分比<br/>
	 * 取值范围：0~n
	 */
	protected int condLeftHp;

	/**
	 * 评星条件：死亡单位数量<br/>
	 * 即死亡单位数量必须小于（等于）该数量，为-1表示不限制<br>
	 * 取值范围：-1~n
	 */
	protected int condDiedCount;

	/**
	 * 评星条件：回合数<br/>
	 * 即实际回合数必须小于（等于）该数值，为0表示不限制<br/>
	 * 取值范围：0~n
	 */
	protected int condRounds;

	/**
	 * 评星条件：时间（秒）<br/>
	 * 即战斗花费时间必须小于（等于）该数值，为0表示不限制<br/>
	 * 取值范围：0~n
	 */
	protected int condTimeElapse;

//	public int[] getNpcTeamIds() {
//		return npcTeamIds;
//	}
//
//	public void setNpcTeamIds(int[] npcTeamIds) {
//		this.npcTeamIds = npcTeamIds;
//		this.npcTeams = null;
//	}
	public int getNpcTeamId() {
		return npcTeamId;
	}

	public void setNpcTeamId(int npcTeamId) {
		this.npcTeamId = npcTeamId;
	}

	public float[] getNpcTeamRatio() {
		return npcTeamRatio;
	}

	public void setNpcTeamRatio(float[] npcTeamRatio) {
		this.npcTeamRatio = npcTeamRatio;
	}

	public int[] getBuffs() {
		return buffs;
	}

	public void setBuffs(int[] buffs) {
		this.buffs = buffs;
	}

	public int[] getDropId() {
		return dropId;
	}

	public void setDropId(int[] dropId) {
		this.dropId = dropId;
	}

	public int getPeakCoin() {
		return peakCoin;
	}

	public void setPeakCoin(int peakCoin) {
		this.peakCoin = peakCoin;
	}

	public int getCondLeftHp() {
		return condLeftHp;
	}

	public void setCondLeftHp(int condLeftHp) {
		this.condLeftHp = condLeftHp;
	}

	public int getCondDiedCount() {
		return condDiedCount;
	}

	public void setCondDiedCount(int condDiedCount) {
		this.condDiedCount = condDiedCount;
	}

	public int getCondRounds() {
		return condRounds;
	}

	public void setCondRounds(int condRounds) {
		this.condRounds = condRounds;
	}

	public int getCondTimeElapse() {
		return condTimeElapse;
	}

	public void setCondTimeElapse(int condTimeElapse) {
		this.condTimeElapse = condTimeElapse;
	}
	
	public NpcTeamTemplate[] getNpcTeams() {
		if (npcTeams == null && npcTeamId > 0) {
			float[] ratios = new float[] { npcTeamRatio[0], 1f, npcTeamRatio[1] };
			Arrays.sort(ratios);
			TemplateService service = App.getApp().getServiceManager().get(TemplateService.class);
			NpcTeamTemplate base = service.get(NpcTeamTemplate.class, npcTeamId); // 基础模板
			npcTeams = new NpcTeamTemplate[3]; // 简单、中等、困难
			for (int i = 0; i < ratios.length; i++) {
				float ratio = ratios[i];
				if (ratio == 1f) {
					npcTeams[i] = base;
				} else {
					NpcTeamTemplate npcTeam = new NpcTeamTemplate();
					npcTeam.setId(base.getId());
					NpcTemplate boss = base.getBoss();
					if (boss != null) {
						npcTeam.setBoss(new NpcTemplate(boss, ratio));
					}
					NpcTemplate[] npcs = base.getNpcs();
					if (npcs != null) {
						NpcTemplate[] _npcs = new NpcTemplate[npcs.length];
						for (int j = 0; j < npcs.length; j++) {
							NpcTemplate npc = npcs[i];
							if (npc != null) {
								_npcs[j] = new NpcTemplate(npc, ratio);
							}
						}
						npcTeam.setNpcs(_npcs);
					}
					npcTeams[i] = npcTeam;
				}
			}
		}
		return npcTeams;
	}
	
	public NpcTeamTemplate getNpcTeam(int difficulty) {
		NpcTeamTemplate[] npcTeams = getNpcTeams();
		return npcTeams != null && difficulty >= 0 && difficulty < npcTeams.length ? npcTeams[difficulty] : null;
	}
	
	public CombatAttribute[] getWrappedBuffs() {
		return wrappedBuffs;
	}
	
	public CombatAttribute getBuff(int index) {
		return wrappedBuffs != null && index >= 0 && index < wrappedBuffs.length ? wrappedBuffs[index] : null;
	}
	
	public DropGroup getDropGroup(int difficulty) {
		if (dropGroups == null) {
			dropGroups = new DropGroupTemplate[dropId.length];
			TemplateService service = App.getApp().getServiceManager().get(TemplateService.class);
			for (int i = 0; i < dropId.length; i++) {
				dropGroups[i] = service.get(DropGroupTemplate.class, dropId[i]);
			}
		}
		DropGroupTemplate template = difficulty >= 0 && difficulty < dropGroups.length ? dropGroups[difficulty] : null;
		return template != null ? template.getDrop() : null;
	}
	
	public byte getType() {
		if (type == -1) {
			type = getNpcTeams() != null ? FLOOR_CHALLENGE : FLOOR_REWARD;
		}
		return type;
	}

	@Override
	public void validate() throws TemplateException {
		if (npcTeamId > 0 && (npcTeamRatio == null || npcTeamRatio.length != 2)) {
			throw new TemplateException("peak_road.xlsx error, npcTeamRatio error: " + id);
		}
		if (buffs != null && buffs.length > 0) {
			wrappedBuffs = new CombatAttribute[buffs.length >> 1];
			for (int i = 0; i < wrappedBuffs.length; i++) {
				CombatAttributeType type = CombatAttributeType.forType(buffs[i << 1]);
				float value = buffs[(i << 1) + 1];
				wrappedBuffs[i] = new CombatAttribute(type, value);
			}
		}
		buffs = null;
	}
	
}
